<template>
  <!-- 模具出入库记录表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top"
      title=""
      :column="2"
      size="large"
      v-show="props.isReadonly"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">数据版本号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.version }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">任务详情id</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.taskDetailId }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具编号</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.mouldCode }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.mouldName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具类型</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.mouldType }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具位置id</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.locationId }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">模具位置名称</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.locationName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">出入库类型</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.outinboundType }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">合格状态;1.合格；0.不合格</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.passStatus }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">出入库时间;1.出库；2.入库</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.outinboundTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">备注</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.remarks }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.createTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.createdUserId }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">创建人名</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.createdUserName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新时间</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.lastUpdateTime }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.lastUpdatedUserId }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">更新人名</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.lastUpdatedUserName }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">是否停用</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.enabled }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">是否删除</div>
        </template>
        <div style="width: 50%; word-break: break-all">
          {{ formData.deleted }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="160"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="数据版本号" prop="version" label-width="80px">
            <el-input
              v-model="formData.version"
              placeholder="请输入数据版本号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="任务详情id"
            prop="taskDetailId"
            label-width="80px"
          >
            <el-input
              v-model="formData.taskDetailId"
              placeholder="请输入任务详情id"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具编号" prop="mouldCode" label-width="80px">
            <el-input
              v-model="formData.mouldCode"
              placeholder="请输入模具编号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具名称" prop="mouldName" label-width="80px">
            <el-input
              v-model="formData.mouldName"
              placeholder="请输入模具名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具类型" prop="mouldType" label-width="80px">
            <el-input
              v-model="formData.mouldType"
              placeholder="请输入模具类型"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具位置id" prop="locationId" label-width="80px">
            <el-input
              v-model="formData.locationId"
              placeholder="请输入模具位置id"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="模具位置名称"
            prop="locationName"
            label-width="80px"
          >
            <el-input
              v-model="formData.locationName"
              placeholder="请输入模具位置名称"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="出入库类型"
            prop="outinboundType"
            label-width="80px"
          >
            <el-input
              v-model="formData.outinboundType"
              placeholder="请输入出入库类型"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="合格状态;1.合格；0.不合格"
            prop="passStatus"
            label-width="80px"
          >
            <el-input
              v-model="formData.passStatus"
              placeholder="请输入合格状态;1.合格；0.不合格"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="出入库时间;1.出库；2.入库"
            prop="outinboundTime"
            label-width="80px"
          >
            <el-input
              v-model="formData.outinboundTime"
              placeholder="请输入出入库时间;1.出库；2.入库"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks" label-width="80px">
            <el-input
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间" prop="createTime" label-width="80px">
            <el-input
              v-model="formData.createTime"
              placeholder="请输入创建时间"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建人" prop="createdUserId" label-width="80px">
            <el-input
              v-model="formData.createdUserId"
              placeholder="请输入创建人"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="创建人名"
            prop="createdUserName"
            label-width="80px"
          >
            <el-input
              v-model="formData.createdUserName"
              placeholder="请输入创建人名"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="更新时间"
            prop="lastUpdateTime"
            label-width="80px"
          >
            <el-input
              v-model="formData.lastUpdateTime"
              placeholder="请输入更新时间"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="更新人"
            prop="lastUpdatedUserId"
            label-width="80px"
          >
            <el-input
              v-model="formData.lastUpdatedUserId"
              placeholder="请输入更新人"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="更新人名"
            prop="lastUpdatedUserName"
            label-width="80px"
          >
            <el-input
              v-model="formData.lastUpdatedUserName"
              placeholder="请输入更新人名"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否停用" prop="enabled" label-width="80px">
            <el-input
              v-model="formData.enabled"
              placeholder="请输入是否停用"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否删除" prop="deleted" label-width="80px">
            <el-input
              v-model="formData.deleted"
              placeholder="请输入是否删除"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESEAMMOULDOUTINBOUNDRECORDADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  version: [],
  taskDetailId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  mouldCode: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  mouldName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  mouldType: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  locationId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  locationName: [
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  outinboundType: [],
  passStatus: [],
  outinboundTime: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  taskDetailId: "",
  mouldCode: "",
  mouldName: "",
  mouldType: "",
  locationId: "",
  locationName: "",
  outinboundType: "",
  passStatus: "",
  outinboundTime: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        taskDetailId,
        mouldCode,
        mouldName,
        mouldType,
        locationId,
        locationName,
        outinboundType,
        passStatus,
        outinboundTime,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        taskDetailId,
        mouldCode,
        mouldName,
        mouldType,
        locationId,
        locationName,
        outinboundType,
        passStatus,
        outinboundTime,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.taskDetailId = "";
  formData.mouldCode = "";
  formData.mouldName = "";
  formData.mouldType = "";
  formData.locationId = "";
  formData.locationName = "";
  formData.outinboundType = "";
  formData.passStatus = "";
  formData.outinboundTime = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 模具出入库记录表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/eammouldoutinboundrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
